import { hexMD5 } from "./md5.js"
import func from "./util.js"
var dev = true;
if (dev === true) {//开发环境
  var baseUrl = "https://zdhapi.fly-network.com/api/";
} else {//生产环境
  var baseUrl = "https://api.yaofangwang.com";
}
//通用请求接口
function request(url, data, type) {
  return new Promise((resolve, reject) => {
    const obj = { method: url, ...data, timestamp: func.dateFormat(Number(new Date())), sessionid: wx.getStorageSync('sessionid') || '', api_key: "4c82ea3cbc33ec41a0f6d37900b93d95", api_secret: "f8fb545fb7bbb025ea0d25c3417c702e" }
    let arr = ''
    Object.keys(obj).map((item, index) => {
      arr += `${item}=${obj[item]}${Object.keys(obj).length - 1 === index ? '' : '&'}`
    })
    const res = { sign: hexMD5(arr) }
    wx.request({
      method: type,
      url: baseUrl,
      data: { ...obj, timestamp: func.dateFormat(Number(new Date())), ...res },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: function (res) {
        if (res.statusCode === 500) {
          wx.showToast({
            title: '服务器错误',
            icon: "none"
          })
          return;
        } else {
          if (res.data.code == -99 || res.data.code == '-2') {
            wx.clearStorageSync();
            wx.reLaunch({
              url: '/pages/wechat/index',
            })
          }
          if (res.data.code == '-1') {
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
          }
        }
        resolve(res.data);
      },
      fail: function (res) {
        reject(res);
      }
    })
  });
}
//接口
// 登录
export const wxToken = (data) => {
  return request('do_login', data, 'POST')
}
// 获取用户信息
export const getInfo = (data) => {
  return request('get_accountinfo', data, 'GET')
}
// 获取首页banner
export const getBanner = (data) => {
  return request('get_dkindex_banner', data, 'GET')
}
// 获取首页
export const getDataList = (data) => {
  return request('get_index_data', data, 'GET')
}
// 获取打卡首页信息
export const getDkList = (data) => {
  return request('get_dkindex_data', data, 'GET')
}
//获取个人打卡详情

export const getDkDetail = (data) => {
  return request('get_dkdetail', data, 'GET')
}
//获取打卡排行榜信息
export const getDKLeaderboard = (data) => {
  return request('get_dkLeaderboard', data, 'GET')
}
//获取他人打卡列表
export const getDKotheruserlist = (data) => {
  return request('get_dkotheruserlist', data, 'GET')
}
// 获取行政区/打卡地区接口
export const getXzqList = (data) => {
  return request('get_area', data, 'GET')
}
// 获取捐书点地址接口
export const getJsdList = (data) => {
  return request('get_donate_school', data, 'GET')
}
// 提交我要捐书信息接口
export const getJSinfo = (data) => {
  return request('post_JSinfo', data, 'POST')
}
// 获取捐书证书接口
export const getJszsimg = (data) => {
  return request('get_jszsimg', data, 'GET')
}
// 获取捐书证书接口
export const setZan = (data) => {
  return request('post_DKclick', data, 'GET')
}
// 获取个人信息
export const getPersonInfodList = (data) => {
  return request('get_accountinfo', data, 'GET')
}

// 获取年级信息接口
export const getGrade = (data) => {
  return request('get_grade', data, 'GET')
}
// 获取打卡详细地址接口
export const getPlace = (data) => {
  return request('get_place', data, 'GET')
}
// 上传文件（视频，图片）
export const uploadFfile = (data) => {
  return request('upload_file', data, 'GET')
}
// 提交打卡信息接口
export const postDKinfo = (data) => {
  return request('post_DKinfo', data, 'POST')
}
// 提交我要投稿信息接口
export const postZPinfo = (data) => {
  return request('post_ZPinfo', data, 'POST')
}
// 提交我要投稿信息接口
export const checkInfo = (data) => {
  return request('get_check_userinfo', data, 'GET')
}
// 获取打卡详情
export const getDKdetailList = (data) => {
  return request('get_DKlist', data, 'GET')
}
// 获取个人作品列表
export const getZPlist = (data) => {
  return request('get_ZPlist', data, 'GET')
}

// 获取个人作品详情
export const ZPdetail = (data) => {
  return request('get_ZPdetail', data, 'GET')
}


// 获取个人资料信息
export const accountinfo = (data) => {
  return request('get_accountinfo', data, 'GET')
}

// 保存个人资料信息
export const userinfo = (data) => {
  return request('post_userinfo', data, 'GET')
}

// 获取奖品列表
export const productlist = (data) => {
  return request('get_productlist', data, 'GET')
}


// 保存领奖信息
export const productinfo = (data) => {
  return request('post_productinfo', data, 'GET')
}

// 获取捐书证书
export const jszsimg = (data) => {
  return request('get_jszsimg', data, 'GET')
}

// 获取打卡证书
export const dkzsimg = (data) => {
  return request('get_dkzsimg', data, 'GET')
}


